﻿
// UsartDlg.cpp: 实现文件
//
#include <string.h>
#include <iostream>
#include <cstdio>
using namespace std;

#include "pch.h"
#include "framework.h"
#include "Usart.h"
#include "UsartDlg.h"
#include "afxdialogex.h"
#include "SerialPort.h"
#include "CTESTDLG.h"



#ifdef _DEBUG
#define new DEBUG_NEW
#endif

bool ComIsOK = FALSE; //串口打开状态标识,为真表示已打开,否则未打开
bool Static_OK = FALSE; //左下角文本闪烁有效
CSerialPort com1;//类对象？
COMM_PORT active_comm_ports[256];//这是自己定义的结构体，每个就是一个串口
CString strIn,strout,message,Time_strIn;
long m_intRxCnt;
UINT ThreadCheck(LPVOID pParam);

int f;//串口数量
int x = 0;//日志标志
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CUsartDlg 对话框



CUsartDlg::CUsartDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_USART_DIALOG, pParent)
	//, m_tx(_T(""))
	//, m_re(FALSE)
	//, m_hex_rx(FALSE)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CUsartDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_BAUD, m_baud);
	DDX_Control(pDX, IDC_BSTOP, m_bstop);
	DDX_Control(pDX, IDC_BDATA, m_bdata);
	DDX_Control(pDX, IDC_CAL, m_cal);
	DDX_Control(pDX, IDC_COMLIST, m_comlist);
	DDX_Control(pDX, IDC_COMCONTROL, m_comcontrol);
	DDX_Control(pDX, IDC_BTN_CLRRX, m_clrrx);
	DDX_Control(pDX, IDC_BTN_HANDSEND, m_handsend);
	DDX_Control(pDX, IDC_BTN_CLRTX, m_clrtx);
	DDX_Control(pDX, IDC_BTN_AUTOSEND, m_autosend);
	DDX_Control(pDX, IDC_EDIT_TIMER, m_timer);
	DDX_Control(pDX, IDC_BTN_SELCTFILE, m_selctfile);
	DDX_Control(pDX, IDC_BTN_SENDFILE, m_sendfile);
	DDX_Control(pDX, IDC_EDIT_STATUS, m_status);
	DDX_Control(pDX, IDC_EDIT_FILEPATH, m_filepath);
	DDX_Control(pDX, IDC_EDIT_RX, m_rx);
	DDX_Control(pDX, IDC_CLOSECOMMLIST, m_closecommlist);
	DDX_Control(pDX, IDC_EDIT_TX, m_tx);
	//  DDX_Check(pDX, IDC_CHECK3, m_re);
	DDX_Control(pDX, IDC_MESSAGE, m_message);
	//  DDX_Check(pDX, IDC_CHECK1, m_hex_rx);
	DDX_Control(pDX, IDC_CHECK3, m_re);
	DDX_Control(pDX, IDC_CHECK1, m_hex_rx);
	DDX_Control(pDX, IDC_CHECK2, m_hex_tx);
}

BEGIN_MESSAGE_MAP(CUsartDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_STN_CLICKED(IDC_STATIC_ICON, &CUsartDlg::OnStnClickedStaticIcon)
	ON_BN_CLICKED(IDC_COMCONTROL, &CUsartDlg::OnBnClickedComcontrol)
	ON_BN_CLICKED(IDC_BTN_CLRRX, &CUsartDlg::OnBnClickedBtnClrrx)
	ON_BN_CLICKED(IDC_BTN_HANDSEND, &CUsartDlg::OnBnClickedBtnHandsend)
	ON_BN_CLICKED(IDC_CLOSECOMMLIST, &CUsartDlg::OnBnClickedClosecommlist)
//	ON_WM_GETMINMAXINFO()
ON_WM_GETMINMAXINFO()
ON_BN_CLICKED(IDC_CHECK1, &CUsartDlg::OnBnClickedCheck1)
ON_BN_CLICKED(IDC_CHECK3, &CUsartDlg::OnBnClickedCheck3)
ON_BN_CLICKED(IDC_BTN_CLRTX, &CUsartDlg::OnBnClickedBtnClrtx)
ON_EN_CHANGE(IDC_EDIT_RX, &CUsartDlg::OnEnChangeEditRx)
ON_BN_CLICKED(IDC_CHECK2, &CUsartDlg::OnBnClickedCheck2)
ON_BN_CLICKED(IDC_BTNEXTEND, &CUsartDlg::OnClickedBtnextend)
ON_WM_TIMER()
END_MESSAGE_MAP()


// CUsartDlg 消息处理程序
//==========静态框闪烁=================================================================== 
UINT ThreadStatic(LPVOID pParam)
{

	while (1)
	{
		if (Static_OK == TRUE)//即当串口数目法师变化时执行此条件语句
		{

			SendMessage(::AfxGetMainWnd()->m_hWnd, WM_STATIC_BLINK, 1, 0); //发送消息,已读到 
		}
		Sleep(100);
	}
	return 0;
}
//=============================================================================================

BOOL CUsartDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	//ShowWindow(SW_MAXIMIZE);

	//ShowWindow(SW_MINIMIZE);

	// TODO: 在此添加额外的初始化代码
	m_comlist.EnableWindow(true); //设置串口号下拉框可用
	m_comlist.SendMessage(CB_SETDROPPEDWIDTH, 200, 0);//下拉框宽度设置
	//m_comlist.SetHorizontalExtent(1500);//
	m_baud.EnableWindow(true); //设置波特率下拉框可用
	m_bdata.EnableWindow(true); //设置数据位下拉框可用
	m_bstop.EnableWindow(true); //设置停止位下拉框可用
	m_cal.EnableWindow(true); //设置校验位下拉框可用
	GetDlgItem(IDC_BTN_HANDSEND)->EnableWindow(false); //设置手动发送按钮不可用
	GetDlgItem(IDC_BTN_AUTOSEND)->EnableWindow(false); //设置自动发送按钮不可用
	GetDlgItem(IDC_EDIT_TIMER)->EnableWindow(false); //设置发送间隔按钮不可用
	GetDlgItem(IDC_BTN_SELCTFILE)->EnableWindow(false); //设置选择文件按钮不可用
	GetDlgItem(IDC_BTN_SENDFILE)->EnableWindow(false); //设置发送文件按钮不可用
	m_bdata.SetCurSel(3);
	m_cal.SetCurSel(0);
	m_bstop.SetCurSel(0);
	m_baud.SetCurSel(6);


	
	pReceiveThread = AfxBeginThread(ThreadCheck, this, THREAD_PRIORITY_LOWEST);
	int m;
	CString addname;
	
	f= com1.EnumerateCommPorts();//函数获取的是串口数的值，函数是自己编写的.
	for(m=0;m<f;m++)
	{
        com1.GetCommPortsName(m, addname);
		m_comlist.AddString(addname);
		//m_comlist.AddString((LPCTSTR)com1.active_comm_ports[0].test);
	}
	

	//扩展窗设定
	//GetWindowRect(&m_rect_raw);
	/////// IDC_STATIC_TITLE,静态文本Static的ID
	//GetDlgItem(IDC_BTNEXTEND)->GetWindowRect(&m_rtFace);
	//Ext_Width = m_rect_raw.right - m_rtFace.right;
	//OnClickedBtnextend();
	
	// 静态文本闪烁
	pStaticTxtThread = AfxBeginThread(ThreadStatic, this, THREAD_PRIORITY_LOWEST);
	//启动说明，借用rx编辑框
	strIn =  TEXT("==================    神秘串口助手V1.01    ================= \r\n");
	strIn += TEXT("说明：       一个简单的串口助手，具备一些最基本的功能\r\n");
	strIn += TEXT("V1.02更新：  新增了16进制收发功能\r\n\r\n");
	strIn += TEXT("V1.01更新：  在V1.0的基础上修改了一些地方，避免了一些可能卡死的操作\r\n\r\n");
	strIn += TEXT("作者：	湖南农业大学物联网工程 >>Lee<<\r\n");
	OnCheckHexrx();



	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CUsartDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CUsartDlg::OnPaint()
{

	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CUsartDlg::OnQueryDragIcon()
{

	return static_cast<HCURSOR>(m_hIcon);

}



void CUsartDlg::OnOK()
{
	// TODO: 在此添加专用代码和/或调用基类

	// CDialogEx::OnOK(); //不注释的话 在窗口中按回车会直接退出
}


void CUsartDlg::OnStnClickedStaticIcon()
{
	// TODO: 在此添加控件通知处理程序代码
}

//==========串口监听线程函数=================================================================== 
UINT ThreadFunc(LPVOID pParam)
{

	// CCommassistDlg* pdlg = (CCommassistDlg*)pParam; //定义指针指向主对话框 
	COMSTAT ComStat;
	DWORD dwErrorFlags;

	while (ComIsOK)//如果串口打开
	{
		DWORD dwBytesRead = 100;
		ClearCommError(com1.m_ucom, &dwErrorFlags, &ComStat);
		dwBytesRead = min(dwBytesRead, (DWORD)ComStat.cbInQue);
		if (!dwBytesRead)//
		{
			Sleep(10);//continue;//使用continue时，打开串口后CPU占用率非常高 
		}
		else ::SendMessage(::AfxGetMainWnd()->m_hWnd, WM_READCOMM, 1, 0); //发送消息,已读到 
	}
		
	return 0;
}
//=============================================================================================

//==========串口检测线程函数=================================================================== 
UINT ThreadCheck(LPVOID pParam)
{

	while (1)
	{
	if (com1.EnumerateCommPorts() != f)//即当串口数目法师变化时执行此条件语句
	{
		SendMessage(::AfxGetMainWnd()->m_hWnd, WM_CHECKPORT, 1, 0); //发送消息,已读到 
		f = com1.EnumerateCommPorts();

	}
	Sleep(100);
	}
	return 0;
}
//=============================================================================================


void CUsartDlg::OnBnClickedComcontrol()
{
	// TODO: 在此添加控件通知处理程序代码
	CString sPortName;//用一个cstring来存储串口名字
	int nBaud, nData, nStop, nCal, nTemp;//波特率，数据位，
	CString sTemp, siTemp;
	int i = 0;
//波特率设置
	nTemp = m_baud.GetCurSel();
	switch (nTemp)
	{
	case 0:
		nBaud = CBR_110;
		break;
	case 1:
		nBaud = CBR_300;
		break;
	case 2:
		nBaud = CBR_600;
		break;
	case 3:
		nBaud = CBR_1200;
		break;
	case 4:
		nBaud = CBR_2400;
		break;
	case 5:
		nBaud = CBR_4800;
		break;
	case 6:
		nBaud = CBR_9600;
		break;
	case 7:
		nBaud = CBR_14400;
		break;
	case 8:
		nBaud = CBR_19200;
		break;
	case 9:
		nBaud = CBR_38400;
		break;
	case 10:
		nBaud = CBR_56000;
		break;
	case 11:
		nBaud = CBR_57600;
		break;
	case 12:
		nBaud = CBR_115200;
		break;
	case 13:
		nBaud = CBR_128000;
		break;
	case 14:
		nBaud = CBR_256000;
		break;
	}

//数据位设置
	nTemp = m_bdata.GetCurSel();
	switch (nTemp)
	{
	case 0:
		nData = 5;
		break;
	case 1:
		nData = 6;
		break;
	case 2:
		nData = 7;
		break;
	case 3:
		nData = 8;
		break;
	}

//停止位下拉框设置
	nTemp = m_bstop.GetCurSel();
	switch (nTemp)
	{
	case 0:
		nStop = ONESTOPBIT;
		break;
	case 1:
		nStop = ONE5STOPBITS;
		break;
	case 2:
		nStop = TWOSTOPBITS;
		break;
	}

//效验位
	nTemp = m_cal.GetCurSel();
	switch (nTemp)
	{
	case 0:
		nCal = NOPARITY;
		break;
	case 1:
		nCal = ODDPARITY;
		break;
	case 2:
		nCal = EVENPARITY;
		break;
	case 3:
		nCal = MARKPARITY;
		break;
	case 4:
		nCal = SPACEPARITY;
		break;
	}
CString s;

	if (m_comlist.GetCurSel() == CB_ERR) //获取串口号选中下标，如果没有选中，则不继续执行
	{
		Static_OK = TRUE;
		m_message.ShowWindow(FALSE);
		m_message.SetWindowTextW(TEXT("先选择一个可用的串口????"));
		return;
	}
	else if (com1.openPort(com1.active_comm_ports[m_comlist.GetCurSel()].CommName, nBaud) )//打开串口，需要串口文件名以及一个波特率
	{
		m_message.SetWindowTextW(TEXT("Done!"));
		ComIsOK = true;
	}
	if (ComIsOK == true)
	{
		CEdit* edit = (CEdit*)GetDlgItem(IDC_COMCONTROL);
		edit->ShowWindow(FALSE); //隐藏"打开串口"控件
		edit = (CEdit*)GetDlgItem(IDC_CLOSECOMMLIST);
		edit->ShowWindow(TRUE); //显示"关闭串口"控件
		//ReadComm();
		pReceiveThread = AfxBeginThread(ThreadFunc, this, THREAD_PRIORITY_LOWEST);

		//m_comcontrol.SetText("关闭串口"); //按钮显示状态改变
		//m_comcontrol.SetForeColor(RGB(0, 155, 0)); //串口打开后文本颜色变绿
		//m_ctrlIcon.SetIcon((HICON)LoadImage(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDI_ICON_OPEN), IMAGE_ICON, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CXICON), 0)); //显示打开icon
		m_comlist.EnableWindow(false); //设置串口号下拉框不可用
		m_baud.EnableWindow(false); //设置波特率下拉框不可用
		m_bdata.EnableWindow(false); //设置数据位下拉框不可用
		m_bstop.EnableWindow(false); //设置停止位下拉框不可用
		m_cal.EnableWindow(false); //设置校验位下拉框不可用
		GetDlgItem(IDC_BTN_HANDSEND)->EnableWindow(true); //设置手动发送按钮不可用
		GetDlgItem(IDC_BTN_AUTOSEND)->EnableWindow(true); //设置自动发送按钮可用
		GetDlgItem(IDC_EDIT_TIMER)->EnableWindow(true); //设置发送间隔按钮可用
		GetDlgItem(IDC_BTN_SELCTFILE)->EnableWindow(true); //设置选择文件按钮可用
		GetDlgItem(IDC_BTN_SENDFILE)->EnableWindow(true); //设置发送文件按钮可用
	}
	else
	{
		s.Format(TEXT("串口打开失败或已被占用！"));
		MessageBox(s);
		return;
	}

}
//=============================================================================================

//==========文本框清除函数=====================================================================
void CUsartDlg::OnBnClickedBtnClrrx()
{
	// TODO: 在此添加控件通知处理程序代码
	com1.clearCommBuf();
	strIn = "";
	Time_strIn = "";
	m_rx.SetWindowTextW(TEXT(""));
}
//=============================================================================================

//==========Windows消息响应函数================================================================ 
int statictxt_timer_cnt = 0;
LRESULT CUsartDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
	// TODO: 在此添加专用代码和/或调用基类

	switch (message)
	{
	case WM_CHECKERROR:
	{
		break;
	}
	case WM_CHECKPORT:
	{
		int f, m;
		CString addname;
		m_comlist.ResetContent();
		f = com1.EnumerateCommPorts();//函数获取的是串口数的值，函数是自己编写的.
		for (m = 0; m < f; m++)
		{
			com1.GetCommPortsName(m, addname);
			m_comlist.AddString(addname);
			//m_comlist.AddString((LPCTSTR)com1.active_comm_ports[0].test);
		}
		if (ComIsOK == true)
		{
			MessageBox(TEXT("串口列表有更新"));
			ComIsOK = false;
		}
		break;
	}
	case WM_READCOMM:
	{
		//读串口消息
		ReadComm();
		this->SendDlgItemMessage(IDC_EDIT_RX, WM_VSCROLL, SB_BOTTOM, 0); //滚动条始终在底部
		break;
	}
	case WM_STATIC_BLINK:
	{
		//读串口消息
		Static_OK = FALSE;
		SetTimer(1, 250, NULL);
		break;
	}
	}
	return CDialogEx::WindowProc(message, wParam, lParam);
}
//=============================================================================================
// 定时器处理
void CUsartDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (statictxt_timer_cnt == 4) 
	{
		//m_message.SetWindowTextW(TEXT("statictxt == 2"));
		statictxt_timer_cnt = 0;
		m_message.ShowWindow(TRUE);
		// 销毁定时器
		KillTimer(nIDEvent);
	}
	else if (statictxt_timer_cnt % 2 == 0)
	{
		//m_message.SetWindowTextW(TEXT("statictxt == 0"));
		m_message.ShowWindow(TRUE);
		statictxt_timer_cnt++;
	}
	else 
	{
		//m_message.SetWindowTextW(TEXT("statictxt == 1"));
		m_message.ShowWindow(FALSE);
		statictxt_timer_cnt++;
	}
	
	CDialogEx::OnTimer(nIDEvent);
}

//==========手动发送数据函数===================================================================
void CUsartDlg::OnBnClickedBtnHandsend()
{
	// TODO: 在此添加控件通知处理程序代码
	CString strTemp;
	char buff[1025*2-1] = "";
	DWORD size = 1024*2;
	DWORD dwWriteBytes = 1024*2;
	DWORD writeBytes = 1024*2;
	GetDlgItem(IDC_EDIT_TX)->GetWindowText(strout);
	CString2Char(strout, buff);
	char buff_hex[1025 * 2 - 1] = "";
	int chbox_hex_tx = m_hex_tx.GetCheck(); //检测16进制模式复选框是否被勾选
	if (chbox_hex_tx)//如果16进制接收打开 
	{
		int i = 0;
		while (1) {
			if (0 == buff[i]) break;
			sprintf_s(buff_hex + i * 2, sizeof(buff), "%02X", (unsigned char)buff[i]);
			i++;
		}
		//setlocale(LC_ALL, "chs");
		i = 0;
		while (buff_hex[i] != 0)
		{
			buff[i] = buff_hex[i];
			i++;
		}
	}

	if (WriteFile(com1.m_ucom, buff, size, &dwWriteBytes, NULL))//size-一次读多少个字符，得到实际读取多少个字符&dwReadBytes
	{
		writeBytes = dwWriteBytes; // readBytes是实际读到的字符数
	}

}
/*
	* 函数名: CString2Char
	* 参数1: CString str                 待转换字符串
	* 参数2: char ch[]                       转换后将要储存的位置
	* 将Unicode下的CString转换为char*
*/
void CUsartDlg::CString2Char(CString str, char ch[])
{
	int i;
	char* tmpch;
	int wLen = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);//得到Char的长度
	tmpch = new char[wLen + 1];                                             //分配变量的地址大小
	WideCharToMultiByte(CP_ACP, 0, str, -1, tmpch, wLen, NULL, NULL);       //将CString转换成char*


	for (i = 0; tmpch[i] != '\0'; i++) ch[i] = tmpch[i];
	ch[i] = '\0';
}
//=============================================================================================

//==========文本框显示函数=====================================================================
void CUsartDlg::OnCheckHexrx()//
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	CString hexIn;
	CString sTemp;
	int tt = m_re.GetCheck();
	if (tt)
	{
		GetDlgItem(IDC_EDIT_RX)->SetWindowText(Time_strIn);
		//MessageBox(TEXT("hhh"));
	}
	else
	GetDlgItem(IDC_EDIT_RX)->SetWindowText(strIn);
	//将strIn内容放入IDC_EDIT_RX框内,即为显示不转换
	return;
}
//=============================================================================================


//==========读取串口数据函数===================================================================
DWORD CUsartDlg::ReadComm()
{
	x = 0;
	CString t1;
	CString strTemp;
	char buff[1025*2-1]="";
	
	DWORD size = 1024*2;
	DWORD readBytes = 1024*2;
	// read data from serial
	COMMTIMEOUTS readCommTimeOuts;
	memset(&readCommTimeOuts, 0, sizeof(readCommTimeOuts));
	readCommTimeOuts.ReadTotalTimeoutConstant = 100;
	SetCommTimeouts(com1.m_ucom, &readCommTimeOuts);
	DWORD readcnt = 0;
	DWORD dwReadBytes = 1024*2;
	int k = 0;

	char buff_hex[1025*2-1] = "";
	//for (k = 0; k < size; k++) {
	if (ReadFile(com1.m_ucom, buff, size, &dwReadBytes, NULL))//size-一次读多少个字符，得到实际读取多少个字符&dwReadBytes
	{
		readBytes = dwReadBytes; // readBytes是实际读到的字符数
	}
	int chbox_hex_rx = m_hex_rx.GetCheck(); //检测16进制模式复选框是否被勾选
	if (chbox_hex_rx)//如果16进制接收打开 
	{
		int i = 0;
		while (1) {
			if (0 == buff[i]) break;
			sprintf_s(buff_hex + i * 2,sizeof(buff), "%02X", (unsigned char)buff[i]);
			i++;
		}
		//setlocale(LC_ALL, "chs");
		i = 0;
		while (buff_hex[i]!=0)
		{
			buff[i] = buff_hex[i];
			i++;
		}
		buff[readBytes*2-1] = NULL;
	}
	else {
		buff[readBytes] = NULL;
	}


	//t1.Format(TEXT("size=%d readBytes=%d dwReadBytes = %d"), size, readBytes, dwReadBytes),
	//MessageBox(t1);

	strTemp = buff;
	m_intRxCnt += strTemp.GetLength(); //接收到字节数统计
	strIn += strTemp;
	

	CString sss; //获取系统时间
	CTime tm; tm = CTime::GetCurrentTime();
	sss = tm.Format("[%Y-%m-%d  %X]# RECV >\r\n");
	Time_strIn += sss;
	Time_strIn += strTemp;
	int tt = m_re.GetCheck(); //检测日志模式复选框是否被勾选
	if (tt)
	{
		
		strIn = "";
		CString ff = TEXT("\r\n\r\n");
		Time_strIn += ff;
		OnCheckHexrx();

	}
	else 
	{
		Time_strIn = "";
		OnCheckHexrx();
	}
		
	if (size == dwReadBytes)
	return 1;
}
//=============================================================================================

//==========串口关闭控制函数===================================================================
void CUsartDlg::OnBnClickedClosecommlist()
{
	// TODO: 在此添加控件通知处理程序代码
	CEdit* edit = (CEdit*)GetDlgItem(IDC_CLOSECOMMLIST);
	edit->ShowWindow(FALSE); //隐藏"关闭串口"控件
	edit = (CEdit*)GetDlgItem(IDC_COMCONTROL);
	edit->ShowWindow(TRUE); //显示"打开串口"控件

	bool t=com1.closePort();//调用关闭串口函数
	ComIsOK = false;
	//m_comcontrol.SetText("打开串口");
	//m_comcontrol.SetForeColor(RGB(255, 0, 0));
	//m_ctrlIcon.SetIcon((HICON)LoadImage(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDI_ICON_CLOSE), IMAGE_ICON, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CXICON), 0)); //显示关闭icon
	m_comlist.EnableWindow(true); //设置串口号下拉框可用
	m_baud.EnableWindow(true); //设置波特率下拉框可用
	m_bdata.EnableWindow(true); //设置数据位下拉框可用
	m_bstop.EnableWindow(true); //设置停止位下拉框可用
	m_cal.EnableWindow(true); //设置校验位下拉框可用
	GetDlgItem(IDC_BTN_HANDSEND)->EnableWindow(false); //设置手动发送按钮不可用
	GetDlgItem(IDC_BTN_AUTOSEND)->EnableWindow(false); //设置自动发送按钮不可用
	GetDlgItem(IDC_EDIT_TIMER)->EnableWindow(false); //设置发送间隔按钮不可用
	GetDlgItem(IDC_BTN_SELCTFILE)->EnableWindow(false); //设置选择文件按钮不可用
	GetDlgItem(IDC_BTN_SENDFILE)->EnableWindow(false); //设置发送文件按钮不可用

}
//=============================================================================================

//==========控制窗口最小大小===================================================================
void CUsartDlg::OnGetMinMaxInfo(MINMAXINFO* lpMMI)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	lpMMI->ptMinTrackSize.x = 860; // 设定最小宽度
	lpMMI->ptMinTrackSize.y = 600; // 设定最小高度


	CDialogEx::OnGetMinMaxInfo(lpMMI);
}
//=============================================================================================







void CUsartDlg::OnBnClickedCheck1()
{
	// TODO: 在此添加控件通知处理程序代码
}


void CUsartDlg::OnBnClickedCheck3()
{
	// TODO: 在此添加控件通知处理程序代码
	//CString strNumber;
	//int tt = m_re.GetCheck(); //检测日志模式复选框是否被勾选
	//if (tt) {
	//	strNumber.Format(TEXT("m_re打开"));
	//	MessageBox(strNumber);
	//}
	//else {
	//	strNumber.Format(TEXT("m_re关闭"));
	//	MessageBox(strNumber);
	//}
}

//======清空发送区===================================================================
void CUsartDlg::OnBnClickedBtnClrtx()
{
	// TODO: 在此添加控件通知处理程序代码
	GetDlgItem(IDC_EDIT_TX)->SetWindowText(TEXT(""));
}
//===================================================================================

void CUsartDlg::OnEnChangeEditRx()
{
	// TODO:  如果该控件是 RICHEDIT 控件，它将不
	// 发送此通知，除非重写 CDialogEx::OnInitDialog()
	// 函数并调用 CRichEditCtrl().SetEventMask()，
	// 同时将 ENM_CHANGE 标志“或”运算到掩码中。

	// TODO:  在此添加控件通知处理程序代码
}


void CUsartDlg::OnBnClickedCheck2()
{
	// TODO: 在此添加控件通知处理程序代码
}


void CUsartDlg::OnClickedBtnextend()
{
	//// TODO: Add your control notification handler code here
	////扩展窗设定
	//GetWindowRect(&m_rect);

	//CString str;

	//if (GetDlgItemText(IDC_BTNEXTEND, str), str == _T(">"))
	//	SetDlgItemText(IDC_BTNEXTEND, _T("<"));
	//else
	//	SetDlgItemText(IDC_BTNEXTEND, _T(">"));

	//CRect rtSmall;
	//rtSmall.SetRectEmpty();
	//if (rtSmall.IsRectEmpty())
	//{
	//	rtSmall.left = m_rect.left;
	//	rtSmall.top = m_rect.top;
	//	rtSmall.right = m_rect.right - Ext_Width;
	//	rtSmall.bottom = m_rect.bottom;
	//}
	//if (str == "<")
	//{
	//	SetWindowPos(NULL, 0, 0, rtSmall.Width(), rtSmall.Height(), SWP_NOMOVE | SWP_NOZORDER );
	//}
	//else
	//{
	//	m_rect.right = m_rect.right + Ext_Width;
	//	SetWindowPos(NULL, 0, 0, m_rect.Width(), m_rect.Height(), SWP_NOMOVE | SWP_NOZORDER );
	//}
	CTESTDLG dlg;
	dlg.DoModal();
}

